﻿using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using GTI = Gadgeteer.SocketInterfaces;

namespace Gadgeteer.Modules.DFRobotics
{
    /// <summary>
    ///     A OptoRelay module for Microsoft .NET Gadgeteer
    /// </summary>
    public class OptoRelay : Module
    {
        private readonly GTI.DigitalOutput relay1;
        private readonly GTI.DigitalOutput relay2;
        private readonly GTI.DigitalOutput relay3;
        private readonly GTI.DigitalOutput relay4;

        private bool state1;
        private bool state2;
        private bool state3;
        private bool state4;

        // This example implements a driver in managed code for a simple Gadgeteer module.  This module uses a 
        // single GTI.InterruptInput to interact with a button that can be in either of two states: pressed or released.
        // The example code shows the recommended code pattern for exposing a property (IsPressed). 
        // The example also uses the recommended code pattern for exposing two events: Pressed and Released. 
        // The triple-slash "///" comments shown will be used in the build process to create an XML file named
        // GTM.DFRobotics.OptoRelay. This file will provide IntelliSense and documentation for the
        // interface and make it easier for developers to use the OptoRelay module.        

        // -- CHANGE FOR MICRO FRAMEWORK 4.2 and higher --
        // If you want to use Serial, SPI, or DaisyLink (which includes GTI.SoftwareI2C), you must do a few more steps
        // since these have been moved to separate assemblies for NETMF 4.2 (to reduce the minimum memory footprint of Gadgeteer)
        // 1) add a reference to the assembly (named Gadgeteer.[interfacename])
        // 2) in GadgeteerHardware.xml, uncomment the lines under <Assemblies> so that end user apps using this module also add a reference.

        // Note: A constructor summary is auto-generated by the doc builder.
        /// <summary></summary>
        /// <param name="socketNumber">The socket that this module is plugged in to.</param>
        public OptoRelay(int socketNumber)
        {
            // This finds the Socket instance from the user-specified socket number.  
            // This will generate user-friendly error messages if the socket is invalid.
            // If there is more than one socket on this module, then instead of "null" for the last parameter, 
            // put text that identifies the socket to the user (e.g. "S" if there is a socket type S)
            Socket socket = Socket.GetSocket(socketNumber, true, this, null);

            relay1 = GTI.DigitalOutputFactory.Create(socket, Socket.Pin.Three, false, this);
            relay2 = GTI.DigitalOutputFactory.Create(socket, Socket.Pin.Four, false, this);
            relay3 = GTI.DigitalOutputFactory.Create(socket, Socket.Pin.Five, false, this);
            relay4 = GTI.DigitalOutputFactory.Create(socket, Socket.Pin.Six, false, this);
        }


        /// <summary>
        ///     Gets or sets the state of relay 1. Setting the relay to True will result in
        ///     the Normally Open (NO) and COM terminal being connected, and the corresponding LED will turn on.
        ///     Setting the relay to False will result in the Normally Closed (NC) and COM terminal being connected,
        ///     and the LED will turn off.
        /// </summary>
        public bool Relay1
        {
            get { return relay1.Read(); }

            set { relay1.Write(value); }
        }

        /// <summary>
        ///     Gets or sets the state of relay 2. Setting the relay to True will result in
        ///     the Normally Open (NO) and COM terminal being connected, and the corresponding LED will turn on.
        ///     Setting the relay to False will result in the Normally Closed (NC) and COM terminal being connected,
        ///     and the LED will turn off.
        /// </summary>
        public bool Relay2
        {
            get { return relay2.Read(); }

            set { relay2.Write(value); }
        }

        /// <summary>
        ///     Gets or sets the state of relay 3. Setting the relay to True will result in
        ///     the Normally Open (NO) and COM terminal being connected, and the corresponding LED will turn on.
        ///     Setting the relay to False will result in the Normally Closed (NC) and COM terminal being connected,
        ///     and the LED will turn off.
        /// </summary>
        public bool Relay3
        {
            get { return relay3.Read(); }

            set { relay3.Write(value); }
        }

        /// <summary>
        ///     Gets or sets the state of relay 4. Setting the relay to True will result in
        ///     the Normally Open (NO) and COM terminal being connected, and the corresponding LED will turn on.
        ///     Setting the relay to False will result in the Normally Closed (NC) and COM terminal being connected,
        ///     and the LED will turn off.
        /// </summary>
        public bool Relay4
        {
            get { return relay4.Read(); }

            set { relay4.Write(value); }
        }
    }
}